#!/bin/bash

# Check if user is root
if [ $(id -u) != "0" ]; then
    printf "Error: You must be root to run this script!\n"
    exit 1
fi

printf "\n"
printf "==========================\n"
printf " Nginx 1.6.0 Install	  \n"
printf "==========================\n"
printf "\n\n"

if [ ! -s websrc ]; then    
    printf "Error: directory websrc not found.\n"
    exit 1
fi

cd websrc

printf "\n========= source package download start =========\n\n"

if [ -s php-5.5.12.tar.gz ]; then
    echo "php-5.5.12.tar.gz [found]"
else
    echo "php-5.5.12.tar.gz download now..."
    wget http://us1.php.net/get/php-5.5.12.tar.gz/from/this/mirror    
fi

phpMd5=`md5sum php-5.5.12.tar.gz | awk '{print $1}'`
if [ "$phpMd5" != "b6a6e9c72589c265aafb7b3353a34030" ]; then
    echo "Error: php-5.5.12.tar.gz package md5 value is invalid. Please check package download url";
    exit 1
fi

if [ -s libmcrypt-2.5.8.tar.gz ]; then
    echo "libmcrypt-2.5.8.tar.gz [found]"
else
    echo "libmcrypt-2.5.8.tar.gz download now..."
    wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
fi

if [ -s libiconv-1.14.tar.gz ]; then
  echo "libiconv-1.14.tar.gz [found]"
else
  echo "libiconv-1.14.tar.gz download now..."
  wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
fi

if [ -s jpegsrc.v9.tar.gz ]; then
  echo "jpegsrc.v9.tar.gz [found]"
else
  echo "jpegsrc.v9.tar.gz download now..."
  wget http://www.ijg.org/files/jpegsrc.v9.tar.gz
fi

if [ -s libpng-1.6.2.tar.gz ]; then
  echo "libpng-1.6.2.tar.gz [found]"
else
  echo "libpng-1.6.2.tar.gz download now..."
  wget http://prdownloads.sourceforge.net/libpng/libpng-1.6.2.tar.gz
fi

if [ -s freetype-2.4.12.tar.gz ]; then
  echo "freetype-2.4.12.tar.gz [found]"
else
  echo "freetype-2.4.12.tar.gz download now..."
  wget http://download.savannah.gnu.org/releases/freetype/freetype-2.4.12.tar.gz
fi

if [ -s mhash-0.9.9.9.tar.gz ]; then
  echo "mhash-0.9.9.9.tar.gz [found]"
else
    echo "mhash-0.9.9.9.tar.gz download now..."
    wget http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz
fi

if [ -s mcrypt-2.6.8.tar.gz ]; then
  echo "mcrypt-2.6.8.tar.gz [found]"
else
  echo "mcrypt-2.6.8.tar.gz download now..."
  wget http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz
fi

if [ -s autoconf-latest.tar.gz ]; then
  echo "autoconf-latest.tar.gz [found]"
else
  echo "pautoconf-latest.tar.gz download now..."
  wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
fi

if [ -s php-5.5.12 ]; then
    rm -rf php-5.5.12
fi
tar zxvf php-5.5.12.tar.gz

if [ -s libmcrypt-2.5.8 ]; then
    rm -rf libmcrypt-2.5.8
fi
tar zxvf libmcrypt-2.5.8.tar.gz

if [ -s libiconv-1.14 ]; then
    rm -rf libiconv-1.14
fi
tar zxvf libiconv-1.14.tar.gz

if [ -s jpeg-9 ]; then
    rm -rf jpeg-9
fi
tar zxvf jpegsrc.v9.tar.gz

if [ -s libpng-1.6.2 ]; then
    rm -rf libpng-1.6.2
fi
tar zxvf libpng-1.6.2.tar.gz

if [ -s freetype-2.4.12 ]; then
    rm -rf freetype-2.4.12
fi
tar zxvf freetype-2.4.12.tar.gz

if [ -s mhash-0.9.9.9 ]; then
    rm -rf mhash-0.9.9.9
fi
tar zxvf mhash-0.9.9.9.tar.gz

if [ -s mcrypt-2.6.8 ]; then
    rm -rf mcrypt-2.6.8
fi
tar zxvf mcrypt-2.6.8.tar.gz

if [ -s autoconf-2.69 ]; then
    rm -rf autoconf-2.69
fi
tar zxvf autoconf-latest.tar.gz

printf "\n========= source package download completed =========\n\n"
printf "========= libiconv install start... =========\n\n"

cd libiconv-1.14
./configure --prefix=/usr/local
make
make install
cd -

printf "\n========== libiconv install end ==========\n\n"
printf "========= mhash install start... =========\n\n"

cd mhash-0.9.9.9
./configure --prefix=/usr/local
make
make install
cd -

printf "\n========== mhash install end ==========\n\n"

printf "========= libmcrypt install start... =========\n\n"

cd libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install
cd -

isSet=`grep "/usr/local/lib" /etc/ld.so.conf | wc -l`
if [ "$isSet" != "1" ]; then
    echo "/usr/local/lib">>/etc/ld.so.conf
    ldconfig
fi


printf "\n========== libmcrypt install end ==========\n\n"
printf "========= jpeg install start... =========\n\n"

cd jpeg-9
./configure --prefix=/usr/local
make
make install
cd -
printf "\n========== jpeg install end ==========\n\n"
printf "========= libpng install start... =========\n\n"

cd libpng-1.6.2
./configure --prefix=/usr/local
make
make install
cd -

printf "\n========== libpng install end ==========\n\n"
printf "========= freetype install start... =========\n\n"

cd freetype-2.4.12
./configure --prefix=/usr/local
make
make install
cd -

printf "\n========== freetype install end ==========\n\n"
printf "========= autoconf install start... =========\n\n"

cd autoconf-2.69
./configure --prefix=/usr/local
make
make install
cd -

printf "\n========== autoconf install end ==========\n\n"
printf "========= mcrypt install start... =========\n\n"

cd mcrypt-2.6.8
./configure --prefix=/usr/local
make
make install
cd -

printf "\n========== mcrypt install end ==========\n\n"
printf "========= PHP install start... =========\n\n"

cd php-5.5.12
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gd --with-iconv-dir=/usr/local --with-freetype-dir=/usr/local --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-zlib --with-xmlrpc --with-curl --with-openssl --with-mcrypt=/usr/local --with-mhash --with-gettext --with-ldap --with-ldap-sasl --without-pear --enable-xml --enable-session --enable-sockets --enable-zip --enable-mbstring --enable-bcmath --enable-shmop --enable-soap --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-ftp --enable-pcntl --enable-gd-native-ttf --disable-rpath --enable-maintainer-zts --enable-opcache --enable-calendar
make ZEND_EXTRA_LIBS='-liconv'
make test
make install
cp php.ini-production /usr/local/php5/etc/php.ini
ln -s /usr/local/php5/etc/php.ini /etc/php.ini
mv /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod 0755 /etc/rc.d/init.d/php-fpm
cd -

service php-fpm start
chkconfig php-fpm on

printf "\n========== PHP install Completed! ========\n\n"
printf "============== The End. ==============\n"